package com.len.service.impl;

import com.len.base.BaseMapper;
import com.len.base.impl.BaseServiceImpl;
import com.len.config.PrestoDataSource;
import com.len.entity.Dict;
import com.len.mapper.DictMapper;
import com.len.service.LoadsDictService;
import com.len.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author songxiangyang
 * @date 2019/4/3 下午2:48
 * @email songxiangyang@aliyun.com
 */
@Service
public class LoadsDictServiceImpl extends BaseServiceImpl<Dict,String> implements LoadsDictService {
    @Autowired
    private PrestoDataSource prestoDataSource;
    @Autowired
    private DictMapper dictMapper;

    @Override
    public Map<String, String> loadsDictionary(Dict dict) {
        String sql = "";
        if(StringUtils.isNotBlank(dict.getDictType())){
            sql = "select * from lookup.report_lookup where type='"+dict.getDictType()+"'";
        } else {
            sql = "select * from lookup.report_lookup";
        }
        List<LinkedHashMap<Object, Object>> allData = prestoDataSource.getAllData(sql);

        Map<String, String> dicMap = new HashMap<>();
        for(LinkedHashMap<Object, Object> map : allData){
                dicMap.put(map.get("key").toString(), map.get("value").toString());
        }
        return dicMap;
    }

    @Override
    public JsonUtil selectDictList(Dict dict) {
        Example example = new Example(Dict.class);
        example.createCriteria().andEqualTo("dictType", dict.getDictType());
        List<Dict> dicts = dictMapper.selectByExample(example);
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(dicts);
        return jsonUtil;
    }


    @Override
    public BaseMapper<Dict, String> getMappser() {
        return dictMapper;
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return dictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Dict record) {
        record=super.addValue(record,true);
        return dictMapper.insert(record);
    }

    @Override
    public Dict selectByPrimaryKey(String id) {
        return dictMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Dict record) {
        return dictMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Dict record) {
        return dictMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Dict> selectListByPage(Dict dict) {
        return dictMapper.selectListByPage(dict);
    }
}
